/*
 *  Copyright 2007 frank.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
/**
 * Account page which displays account information. This page uses 
 * a treetable, with inline editable fields, and the last field needs
 * to be editable as well. When the last row is filled in, it is added,
 * and a new treetable is displayed. Columns are index, description, from, debit,
 * credit, balance and modify (edit, delete)
  
 */
package org.frv.wispr.web.account;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import org.apache.wicket.PageParameters;
import org.apache.wicket.security.components.ISecurePage;
import org.apache.wicket.security.components.SecureWebPage;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation.Unit;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation.Alignment;
import org.apache.wicket.extensions.markup.html.tree.table.IColumn;
import org.apache.wicket.extensions.markup.html.tree.table.PropertyRenderableColumn;
import org.apache.wicket.extensions.markup.html.tree.table.PropertyTreeColumn;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.extensions.markup.html.tree.table.TreeTable;
import org.frv.wispr.business.AccountEntry;
import org.frv.wispr.business.AccountEntryRepository;
import org.frv.wispr.web.account.PropertyEditableColumn;
import org.frv.wispr.web.account.ModelBean;

/**
 *
 * @author frank
 */
public class AccountPage extends SecureWebPage {
    private TreeTable table;

    @SpringBean(name="accountEntryRepository")
    AccountEntryRepository accountEntryRepository;
    /**
     * Create account page. Currently a mock up, need to figure out how 
     * to decide which account to show
     * @param parameters
     */
    public AccountPage(PageParameters parameters) {
        IColumn columns[] = new IColumn[] {
            new PropertyTreeColumn(new ColumnLocation(Alignment.LEFT, 15, Unit.PERCENT), "Index", "userObject.index"),
            new PropertyEditableColumn(new ColumnLocation(Alignment.MIDDLE, 1, Unit.PROPORTIONAL), "Description", "userObject.description"),
             new PropertyEditableColumn(new ColumnLocation(Alignment.MIDDLE, 1, Unit.PROPORTIONAL), "From", "userObject.from"),
             new PropertyEditableColumn(new ColumnLocation(Alignment.MIDDLE, 1, Unit.PROPORTIONAL), "Credit", "userObject.credit"),
             new PropertyEditableColumn(new ColumnLocation(Alignment.MIDDLE, 1, Unit.PROPORTIONAL), "Debit", "userObject.debit"),
             new PropertyEditableColumn(new ColumnLocation(Alignment.RIGHT, 15, Unit.PERCENT), "Balance", "userObject.balance"),
        };
        
        table = new TreeTable("account", createTreeModel(), columns);
        table.setRootLess(true);
        add(table);
        table.getTreeState().collapseAll();
    }
    
    public TreeModel createTreeModel() {
    	// for the moment not a detachable model
    	Locale loc = new Locale("nl", "NL", "EURO");
    	NumberFormat formatter = NumberFormat.getCurrencyInstance(loc);
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(new ModelBean("ROOT"));
        BigDecimal balance = new BigDecimal("0");
        
    	List<AccountEntry> entries =  accountEntryRepository.findAllByAccountId(0L);
    	for (AccountEntry entry : entries) {
    		ModelBean bean = new ModelBean(entry.getIndex().toString());
    		bean.setDescription(entry.getDescription());
    		if (entry.getCredit() != null) {
    			bean.setCredit(formatter.format(entry.getCredit()));
    			balance = balance.add(entry.getCredit());
    		} else {
    			bean.setCredit("");
    		}
    		if (entry.getDebit() != null) {
    			bean.setDebit(formatter.format(entry.getDebit()));
    			balance = balance.subtract(entry.getDebit());
    		} else {
    			bean.setDebit("");
    		}
    		bean.setFrom(entry.getDoubleEntry().getAccount().getName());
    		bean.setBalance(formatter.format(balance));
    		node.add(new DefaultMutableTreeNode(bean));
    	}
        
    	return new DefaultTreeModel(node);
    }
    
}